All Questions
Tagged with nested-loopsc
271 questions
0votes
4answers
120views
Why does this code write the answers more than once instead of just once?
I've just made a C program about finding perfect numbers. It is kinda working, but it writes out the answers more than once(and it writes 24 as well for some reason). I have very basic C knowledge, I ...
1vote
2answers
74views
C language pattern with unexpected result
I am trying to create a program in C to draw this pattern : 1 1 2 1 3 1 1 2 2 2 3 2 1 3 2 3 3 3 1 4 2 4 3 4 1 5 2 5 3 5 I checked the answer from solutions. #include <stdio.h> int main () { ...
2votes
1answer
34views
Modification pgm fill 2-D Array using a pointer and it doesn't work
Initial pgm void assign( int **mat, int n, int m ) { int **p = mat; int **p_end = p + n; for ( ; p < p_end; ++p ) { int *q = *p; int *q_end = q + m; for ( ; q < q_end; ++q ) { printf( ...
0votes
1answer
53views
Nested strtok() calls to tokenize given string does not work as expected [duplicate]
I want to tokenize a provided input string using strtok(). In the first step, I want to tokenize the given string by "|" symbol. Then, I tokenize each substring by ";". Finally, I ...
0votes
0answers
62views
Nested loop to access each record index
I have a text file that has 4 records and each record consists of 4 values. I also have 3D matrix where each item in this matrix is one of those records. I want to iterate over this matrix and get for ...
2votes
4answers
356views
Print numbers of a matrix in a wave pattern
I want the output to be 7 4 1 2 5 8 9 6 3 but it's coming out to be 1 4 7 2 5 8 3 6 9. How can I fix it, and what's the logic behind it so that I can print the reverse wave pattern, too? #include<...
1vote
3answers
130views
for loop in between if statement is not working in C
#include <stdio.h> int main() { int row, col, i, j; char ch; printf("Enter the Value of Row or Column: "); scanf("%d", &row); col = row; int mat[...
0votes
3answers
59views
error in comparing elements of a 2d array using if-else statements
i tried to make a game using 2d character arrays. but the if else statements are not working and the loop is not breaking. Also when i assign a character to the last element of my row , the next row's ...
1vote
3answers
72views
How do I prevent checking of duplicate elements in a array?
I am checking frequency of each element in an array. The problem is the duplicate elements are counting themselves more than needed, I want to skip the counting procedure of the element which has been ...
0votes
2answers
115views
Else statement logic is wacko
#include <cs50.h> #include <stdio.h> #include <string.h> #include <ctype.h> int main(void) { string key_upper = "YTNSHKVEFXRBAUQZCLWDMIPGJO"; string key_lower =...
0votes
2answers
181views
Why do I get a segmentation fault? The code works fine in CLion and VSC
I am currently using exercism, to improve my programming skills. The code works fine in my IDE, but causes an error message on the website. What did I do wrong and how would you improve my code? Thx ...
-3votes
2answers
110views
Stuck on pset 3 (Runoff) HELPP
Can someone tell me why my code doesn't work. here's its description https://cs50.harvard.edu/x/2020/psets/3/runoff/. the error is most probably outside of the main function. edit - the issue has been ...
1vote
3answers
1kviews
How to use pointers in LeetCode Two Sum problem in C
I am new to LeetCode and my only background in C is a single class of its basics. I wish to improve and learn more. I think my problem here is with the pointers or malloc. How can I solve this? This ...
2votes
4answers
141views
How to exit an outer loop in C (without ++)?
In Java, it is possible to escape from an outer loop using such a construct: int[][] matrix; int value; ... outer: { for(int i=0; i<n; i++) for (int j=0; j<m; j++) if (matrix[i][j] ==...
1vote
1answer
550views
CS50 plurality, printing winner
bool vote(string name) { // TODO for (int i = 0; i < candidate_count; i++) { if (strcmp(candidates[i].name, name) == 0) //W's { candidates[i].votes++; ...